from flask import Flask, render_template, json, request, redirect, session, jsonify, url_for
import plotly as py
import plotly.graph_objs as go
import pandas as pd

app = Flask(__name__)

@app.route("/")
def main():
    pyplt = py.offline.plot
    labels = ["一般期待 一切顺其自然", "非常期待 希望快点遇见对的人", "不期待 对谈恋爱没什么兴趣"]
    values = [48.64, 46.66, 4.7]
    data = [go.Pie(labels=labels, values=values)]
    layout = go.Layout(
        title="单身青年对恋爱的期待指数（%）",
    )
    fig = go.Figure(data=data, layout=layout)
    div = pyplt(fig, output_type='div', include_plotlyjs=False, auto_open=False, show_link=False)

    pyplt = py.offline.plot
    labels = ["3年以上", "1年~3年", "半年~1年", "半年以内", "正在谈恋爱"]
    values = [31.18, 19.27, 19.82, 16.82, 12.91]
    data = [go.Pie(labels=labels, values=values)]
    layout = go.Layout(
        title="距离TA们上一次恋爱过去多久了（%）",
    )
    fig = go.Figure(data=data, layout=layout)
    div2 = pyplt(fig, output_type='div', include_plotlyjs=False, auto_open=False, show_link=False)

    context = {}
    context['graph'] = div
    context['graph2'] = div2
    return render_template('main.html', context=context)


@app.route("/page_second")
def page_second():
    pyplt = py.offline.plot
    labels = ["三观相合 与自己对事物的看法相同", "看感觉，心动就行", '见识谈吐、学历、礼仪', '颜值外形、身材、长相、穿搭', '经济基础、家庭收入']
    values = [38.12, 23.66, 23.55, 9.64, 5.04]
    data = [go.Pie(labels=labels, values=values)]
    layout = go.Layout(
        title="约会对象哪一点最容易让年轻人心动",
    )
    fig = go.Figure(data=data, layout=layout)
    div = pyplt(fig, output_type='div', include_plotlyjs=False, auto_open=False, show_link=False)

    import plotly.express as px
    data = pd.read_csv(r'data/salary.csv')
    fig2 = px.bar(
        data,
        x='Visualization_tools',
        y='salary'
    )
    div2 = pyplt(fig2, output_type='div', include_plotlyjs=False, auto_open=False, show_link=False)

    context = {}
    context['graph3'] = div
    context['graph4'] = div2
    return render_template('second.html', context=context)


@app.route("/page_final")
def page_final():
    return render_template('final.html')


if __name__ == "__main__":
    # 运行 Flask 系统实例
    app.run()
